<template>
  <div class="app-container">

    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入社团名称..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initClubList">搜索</el-button>
      <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete"
            v-if="hasAuth('system:user:delete')">批量删除</el-button>
        </template>
      </el-popconfirm>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="clubId" v-if="false" label="社团id" width="20" />
      <el-table-column prop="filepath" label="封面" width="80" align="center">
        <template v-slot="scope">
          <img :src="getServerUrl() + scope.row.filepath" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column prop="clubNumber" label="社团编号" width="120" align="center" />
      <el-table-column prop="clubName" label="社团名称" width="100" align="center" />
      <el-table-column prop="createName" label="创建人" width="100" align="center" />
      <el-table-column prop="clubCreate" label="成立时间" width="200" align="center" />
      <el-table-column prop="clubMessage" show-overflow-tooltip="true" label="简介" width="200" align="center" />
      <el-table-column prop="clubType" label="社团类型" :formatter="ConvertType" width="120" align="center" />
      <el-table-column prop="clubStatus" label="审核状态" width="200" :formatter="convertStatus" align="center" />
      <el-table-column prop="clubRemark" show-overflow-tooltip="true" label="申请说明" width="200" align="center" />
      <el-table-column prop="createTime" label="创建时间" width="200" align="center" />
      <el-table-column prop="action" label="操作" width="400" fixed="right" align="center">
        <template v-slot="scope">
          <el-button  @click="showById(scope.row.clubId)"
            v-if="hasAuth('system:user:role')">查看详情</el-button>
          <el-button v-if="hasAuth('system:user:edit')" type="primary"
            @click="audit(scope.row.clubId, '1')">通过</el-button>
          <el-button v-if="hasAuth('system:user:edit')" type="danger"
            @click="audit(scope.row.clubId, '2')">不通过</el-button>
          <el-popconfirm title="您确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" :icon="Delete" v-if="hasAuth('system:user:delete')" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:currentPage="queryForm.pageNum" v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
    @initClubList="initClubList"></Dialog>
</template>

<script setup>
import { Search, Delete, DocumentAdd, Edit, Tools, RefreshRight } from '@element-plus/icons-vue'
import { ref } from 'vue'
import requestUtil, { getServerUrl } from "@/utils/request";
import { ElMessage, ElMessageBox } from 'element-plus'
import Dialog from './components/dialog'
import ClubStatusEnum from '@/enum/clubStatusEnum'
import ClubTypeEnum from '@/enum/clubTypeEnum'

const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const auditParam = ref({
  id: 0,
  type: ''
})

const total = ref(0)

const tableData = ref([])

const id = ref(-1)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const multipleSelection = ref([])

const delBtnStatus = ref(true)




/**
 * 状态转换
 * @param {行内容} row 
 * @param {列} column 
 * @param {该行该列的内容} cellValue 
 */
const convertStatus = (row, column, cellValue) => {
  return ClubStatusEnum.getDesc(cellValue);
}

const showById = (clubId) => {
  id.value = clubId;
  dialogVisible.value = true;
}

/**
 * 社团类型枚举转换
 * @param {行内容} row 
 * @param {列} column 
 * @param {该行该列的内容} cellValue 
 */
const ConvertType = (row, column, cellValue) => {
  return ClubTypeEnum.getDesc(cellValue);
}

const handleSelectionChange = (selection) => {
  console.log("勾选了")
  console.log(selection)
  multipleSelection.value = selection;
  delBtnStatus.value = selection.length == 0;
}


const initClubList = async () => {
  const res = await requestUtil.post("/club/listClub", queryForm.value)
  tableData.value = res.data.clubList;
  total.value = res.data.total;
}


initClubList();

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1;
  queryForm.value.pageSize = pageSize;
  initClubList();
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum;
  initClubList();
}

const handleDelete = async (id) => {
  var ids = []
  if (id) {
    ids.push(id)
  } else {
    multipleSelection.value.forEach(row => {
      ids.push(row.id)
    })
  }
  const res = await requestUtil.post("sys/user/delete", ids)
  if (res.data.code == 200) {
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initClubList();
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}

const audit = async (clubId, type) => {
  auditParam.value.id = clubId;
  auditParam.value.type = type;
  const res = await requestUtil.get('/club/auditClub', auditParam.value);
  if (res.data.code === 200) {
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initClubList();
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}

</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;

}

.el-tag--small {
  margin-left: 5px;
}
</style>
